<?php
session_start();
///error_reporting(~E_WARNING);
date_default_timezone_set('Asia/Kolkata');

$hostName = 'localhost';
$userName = 'root';
$password = '';

mysql_connect($hostName,$userName,$password) or die(mysql_error()." <br>Unable to connnect MYSQL");

mysql_select_db('b19')or die(mysql_error()." <br>Unable to Select DATABASE");


function pr($arr = array()) {
  $flag = true;
  if($flag == false)return;
  echo '<pre style="color:white;background:black;">';
  print_r($arr);
  echo '</pre>';
}

function vx($arr = array()) {
  $flag = true;
  if($flag == false)return;
  echo '<pre style="color:white;background:black;">';
  var_export($arr);
  echo '</pre>';
}


/*
 * DB function used to insert data in specific Db Table
 * @param $tableName STRING (Name of Table)
 * @param $daata Array (Data to be inserted)
 * @return INT (last insert ID)
 * Usage : echo insert('users',$_POST);
 * */

function insert($tableName='',$data=array())
{

  if(empty($tableName)|| empty($data) ){
    return "Unable to insert";
  }
   $insertQuery =
 "INSERT INTO `$tableName` SET";

  foreach($data as $colName=>$value) {
    $insertQuery .="`$colName`='$value' ,";
  }

  $insertQuery = rtrim($insertQuery,', ');


  mysql_query($insertQuery)or die(mysql_error()." <br>Unable to INSERT DATA");

  return mysql_insert_id();
  /*
    // How to select LAst inserted KEY
    echo "<br>last Insert Id is :".mysql_insert_id();

    // How to know number of rows affected
    echo "<br>Affected Rows :".mysql_affected_rows();
  */
}


/*
  @param STRING $table = 'users'
  @param STRING $where = "WHERE `name`='php'"
  @param STRING $order = 'ORDER BY `name` DESC'
  @param STRING $limit = 'LIMIT 0,1'
  @param STRING $colName = '`id`,`name`,`email`'
  @return $result STREAM(resource)
*
*/
function fetchAll($table='',$where='',$order='',$limit='',$colName=''){

  $colName = ($colName=='') ? '*' :$colName;
  $query = "SELECT $colName FROM `$table` $where $order $limit";
pr($query);
  $result = mysql_query($query) or die(mysql_error());
  return $result;
}


function getCountries()
{
  return array(
    "in"=>"India",
    "pk"=>"pakistan",
    "np"=>"Nepal",
    "ch"=>"China",
    "bn"=>"BanglaDesh",
    "sl"=>"SriLanka",
    "bh"=>"Bhutan",
    "af"=>"Afganistan",
    "my"=>"Myanmar");
}
